<template>
  <div>
    <div class="pic-list">
      <div
          class="image-item"
          v-for="($item, $index) in imageList"
          :key="$index"
      >
        <div class="img">
          <img v-lazy="fillImg($item.spuImage)" alt="" class="l-img"/>
          <div class="label">
            <el-tooltip class="item" effect="dark" content="删除" placement="top" style="margin-left: 20px">
              <div @click="actionDel($item)">
                <svg-icon icon-class="remove"></svg-icon>
              </div>
            </el-tooltip>
          </div>
        </div>
        <div class="title">{{ $item.spuName }}</div>
        <!--      <div class="collection-product_bottom">
                <span :class="['tag', $item.published === 1 ? 'tag-green': '']">{{
                    $item.published === 1 ? '已上架' : '未上架'
                  }}</span>
              </div>-->
      </div>
    </div>
    <el-pagination
        v-if="currentSpuList.length > 50"
        class="pagination"
        background
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
        :current-page="page"
        :page-sizes="[50, 100, 200, 400, 600]"
        :page-size="50"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>
    <empty style="justify-content: center" v-if="imageList.length === 0"/>

  </div>
</template>

<script>
import {deepClone} from '@/utils/utils';

export default {
    name: 'pic-list',
    props: {
        currentSpuList: {
            type: Array,
            default: () => []
        }
    },
    data () {
        return {
            imageList: [],
            page: 1,
            total: 0,
            pageSize: 50
        };
    },
    watch: {
        currentSpuList: {
            immediate: true,
            deep: true,
            handler (val) {
                if (val) {
                    this.initPageItems();
                }
            }
        }
    },
    methods: {
    // 错误图片显示
        fillImg (src) {
            const match = /(http|https):\/\/([\w.]+\/?)\S*/;
            return !match.test(src) ? `https://tlkj-item-pic.oss-cn-beijing.aliyuncs.com${src}` : src;
        },
        actionDel (item) {
            let tindex = this.currentSpuList.findIndex(res => res.spuId === item.spuId);
            this.currentSpuList.splice(tindex, 1);
            // this.initialCurrentPageSpu();
            this.$emit('update', this.currentSpuList);
        },
        initPageItems (page = this.page) {
            const lists = deepClone(this.currentSpuList);
            this.total = lists.length;
            this.pageCount = Math.ceil(this.total / this.pageSize);
            let offset_start = (page - 1) * this.pageSize;
            let offset_end = page * this.pageSize;
            if (offset_end > this.total) {
                offset_end = this.total;
            }
            this.imageList = lists.slice(offset_start, offset_end);
        },
        handleCurrentChange (val) {
            this.selectItems = [];
            this.page = val;
            this.initPageItems();
        },
        handleSizeChange (val) {
            this.page = 1;
            this.pageSize = val;
            this.initPageItems();
        }
    }
};
</script>

<style scoped lang="scss">
.pic-list {
  width: 100%;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  line-height: 1;
}

.image-item {
  position: relative;
  margin-right: 10px;
  margin-bottom: 10px;
  border: 1px solid #c0ccda;
  border-radius: 6px;
  overflow: hidden;
  cursor: pointer;
  width: 160px;
  //background-color: #f7f7f7;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 10px;
  height: 240px;

  .img {
    position: relative;
    overflow: hidden;

    .l-img {
      width: 135px;
      height: 180px;
      object-fit: cover;
    }

    &:hover {
      .label {
        display: flex;
      }
    }

    .right_top_flag {
      position: absolute;
      top: 0;
      right: 0;
      box-shadow: rgb(31 125 248 / 50%) 0px -69.1px 0px 69.1px;
      background-color: rgba(31, 125, 248, 0.5);
      display: flex;
      flex-direction: column;
      align-items: center;
      color: white;
      transform: translateX(30%) rotate(45deg) translateZ(1px);
      width: 88px;
      transform-origin: 40px 16px;
      justify-content: center;

      .top_flag_wrapper > img {
        width: 20px;
      }
    }
  }

  .title {
    margin: 5px 0;
    color: #30344f;
    font-size: 12px;
    overflow: hidden;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    white-space: normal;
    text-align: center;
    text-overflow: ellipsis;
    width: 100%;
  }

  .label {
    flex-direction: row;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 32px;
    text-align: center;
    position: absolute;
    bottom: 0;
    background-color: #000000;
    color: white;
    opacity: 0.6;
    display: none;
    line-height: 32px;
  }

  &.seleted-pic {
    border: 1px solid #1f7df8;
    background-color: rgba(31, 125, 248, 0.6);

    .title, .top {
      color: #fff;
    }
  }
}

.collection-product_bottom {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;

  .tag {
    padding: 0 7px;
    font-size: 12px;
    line-height: 20px;
    white-space: nowrap;
    border: 1px solid #d9d9d9;
    color: #999;
  }

  .tag-green {
    color: #4bb081;
    background: #ffffff;
    border: 1px solid #4bb081;
  }

  .top {
    color: rgba(31, 125, 248, 1);
    font-size: 12px;
  }
}
</style>
